<template>
  <a-card :bordered="false">
    <a-menu v-model="current" mode="horizontal" style="margin-bottom: 20px">
      <a-menu-item :key="1">
        <router-link :to="{ name: $route.name, query: { id: 1 } }">高校</router-link>
      </a-menu-item>
      <a-menu-item :key="2">
        <router-link :to="{ name: $route.name, query: { id: 2 } }">公益组织</router-link>
      </a-menu-item>
    </a-menu>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="组织名称">
              <a-input v-model="queryParam.organizationName" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="省份">
              <a-select v-model="queryParam.provinceCode">
                <a-select-option value="">省份</a-select-option>
                <a-select-option :value="item.provinceCode" v-for="(item, index) in provinceList" :key="index">{{
                  item.provinceName
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchBtn">查询</a-button>
              <a-button style="margin-left: 8px" @click="queryParam = { province: '' }">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator" style="display: flex">
      <router-link :to="{ name: 'OrganizationAdd' }">
        <a-button type="primary" icon="plus">新建</a-button>
      </router-link>
    </div>

    <a-table
      ref="table"
      :row-key="
        (record) => {
          return record.organizationId
        }
      "
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="logo" slot-scope="photo">
        <!-- {{photo}} -->
        <img :src="photo" width="100px" />
      </span>
      <span slot="status" slot-scope="text, record">
        <a-switch
          checked-children="上架"
          un-checked-children="下架"
          @change="
            (checked) => {
              changeStatus(record.organizationId, checked)
            }
          "
          :checked="record.status == 1 ? true : false"
        />
      </span>
      <span slot="time" slot-scope="text, record">
        <template v-if="record.startedAt && record.endAt">
          开始时间：{{ record.startedAt }}<br />
          结束时间：{{ record.endAt }}
        </template>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <router-link :to="{ name: 'OrganizationEdit', params: { id: record.organizationId } }">修改</router-link>
          <a-divider type="vertical" />
          <!-- <a @click="deleteOne(record.organizationId)">删除</a> -->
        </template>
      </span>
    </a-table>
  </a-card>
</template>
<script>
import Area from '@/views/components/AreaCode'
const ImportExcel = () => import('@/components/Upload/ImportExcel')

const columns = [
  {
    title: '序号',
    dataIndex: 'organizationId'
  },
  {
    title: '组织名称',
    dataIndex: 'organizationName',
    width: '10%'
  },
  {
    title: '组织logo',
    dataIndex: 'organizationLogo',
    scopedSlots: { customRender: 'logo' }
  },
  {
    title: '省份',
    dataIndex: 'provinceName'
  },
  {
    title: '参与人数量',
    dataIndex: 'personCountView'
  },
  {
    title: '积分数量',
    dataIndex: 'creditCountView'
  },
  {
    title: '助力时间',
    scopedSlots: { customRender: 'time' }
  },
  {
    title: '状态',
    dataIndex: 'statusText',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'OrganizationList',
  data() {
    return {
      id: this.$route.query.id || 1,
      current: this.$route.query.id ? [+this.$route.query.id] : [1],
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (
            <div>
              共 <span>{total}</span> 条
            </div>
          )
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      regionData: {
        province: '',
        city: '',
        area: ''
      },
      queryParam: {
        province: ''
      },
      provinceList: [] // 省份
    }
  },
  components: {
    ImportExcel,
    RegionInfo: Area
  },
  created() {
    this.getProvinceData()
    // 获取列表
    this.fetch()
  },
  methods: {
    // 改变每页数量时更新显示
    onChange(pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn() {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch() {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize,
        categoryId: this.current[0]
      }
      this.$common
        .handleGet('/organizations/lists', Object.assign({}, params, this.queryParam))
        .then((data) => {
          this.loading = false
          if (data.code === 200) {
            const result = data.data
            const pagination = { ...this.pagination }
            pagination.total = result.totalInfo.total
            this.data = result.lists
            this.pagination = pagination
            console.log(this)
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 获取省 /admin/common/province
    getProvinceData() {
      this.$common.handlePost('/common/province', {}).then((res) => {
        if (res.code === 200) {
          this.provinceList = res.data
        }
      })
    },
    deleteOne(id) {
      this.handleDelete(id)
    },
    handleDelete(id) {
      const _this = this
      const params = {}
      this.$common
        .delete('/broadcasts/delete/' + id, params, [id])
        .then((res) => {
          if (res) {
            _this.fetch()
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 启用、禁用
    changeStatus(id, status) {
      if (id && status !== '') {
        var url = ''
        if (status) url = '/organizations/enable/'
        else url = '/organizations/disable/'
        this.$common
          .changeField(url + id, {})
          .then((data) => {
            if (data.code === 200) {
              this.$message.success(data.message)
              console.log(data)
              this.fetch()
            } else {
              this.$message.error(data.message)
              console.log(data.message)
            }
          })
          .catch(() => {
            this.$notification.error({
              message: '错误',
              description: '请求信息失败，请重试'
            })
          })
      } else {
        this.$message.error('参数错误')
      }
    }
  }
}
</script>
